import { getUserInfo } from "../services/user"
import { request } from "./request"

export const login = () => {
    return new Promise((resolve, reject) => {
        wx.login({
            success: async res => {
                const response: any = await request({
                    url: '/app/index.php?i=1&c=auth&a=session&do=openid',
                    data: { code: res.code },
                })

                // 缓存 sessionid
                wx.setStorageSync('sessionid', response.sessionid)

                // 获取用户信息
                const userInfo = await getUserInfo()
                resolve(userInfo)
            },
            fail: err => {
                reject(err.errMsg)
                wx.showToast({ title: err.errMsg, icon: 'none', duration: 3000 });
            }
        })
    })
}